package com.yoomiito.app.ui.college.detail

import android.graphics.Typeface
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.*
import android.view.LayoutInflater
import android.view.View
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseLazyFragment
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ResUtil
import kotlinx.android.synthetic.main.item_subject_title.view.*
import kotlinx.android.synthetic.main.layout_rcy.*

class SubjectIntroductionFragment : BaseLazyFragment<SubjectIntroductionPresenter>() {

    private lateinit var mAdapter: SubjectIntroductionAdapter
    override fun getLayoutId(): Int = R.layout.layout_rcy

    override fun newP(): SubjectIntroductionPresenter = SubjectIntroductionPresenter(App.mAppConfig)

    override fun initData_kotlin(view: View?, savedInstanceState: Bundle?) {
        initRcy()
    }

    private fun initRcy() {
        layout_rcy.setBackgroundResource(R.color.color_F4F4F4)
        layout_rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
//        layout_rcy.addItemDecoration(SpacingDecoration(0, DensityUtil.dp2px(10f), true))
        mAdapter = SubjectIntroductionAdapter(null)
        layout_rcy.adapter = mAdapter

        val view = LayoutInflater.from(context).inflate(R.layout.item_subject_title, null, false)
        mAdapter.setHeaderView(view)

    }

    private fun textStyle(content: String): SpannableString {
        val spanText = SpannableString(content)
        val pricePosition = content.indexOf(" ")

        spanText.setSpan(RelativeSizeSpan(1.1f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        spanText.setSpan(StyleSpan(Typeface.BOLD), 1, pricePosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spanText.setSpan(RelativeSizeSpan(2f), 1, pricePosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        spanText.setSpan(ScaleXSpan(DensityUtil.dp2px(1f).toFloat()), pricePosition, pricePosition + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

        spanText.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_999999)), pricePosition + 1, content.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spanText.setSpan(StrikethroughSpan(), pricePosition + 1, content.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        return spanText
    }

    fun setData(goodsInfo: GoodsDetail) {
        view?.videoPriceTv?.text = textStyle("¥${goodsInfo.salePrice} ¥${goodsInfo.reserve_price}")
        view?.videoTitleTv?.text = goodsInfo.title
        mAdapter.setNewData(goodsInfo.desc)
    }

    fun updateInfo(url: String) {
        mAdapter.data[0] = url
        mAdapter.notifyItemChanged(1)
    }

}